package com.zero.init;

import com.zero.entity.HotSearch;
import com.zero.service.HotSearchService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.List;

/**
 * 初始化加载数据
 *
 * @author ：某人的目光
 * @date ： 2019/3/3 1:36
 * @description：${description}
 * @modified By：
 * @version: $version$
 */
@Component
public class InitSystem implements ApplicationContextAware, ServletContextListener {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public void loadData(ServletContext application) {
        HotSearchService hotSearchService = (HotSearchService) applicationContext.getBean("hotSearchService");
        List<HotSearch> hotSearchList = hotSearchService.listAll(Sort.Direction.ASC, "sort");
        System.out.println("初始化数据********************");
        application.setAttribute("hotSearchList", hotSearchList);
    }

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        loadData(sce.getServletContext());
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }

}
